//
//  HomeViewController.swift
//  Weibo
//
//  Created by gdarkness on 16/3/20.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit

class HomeViewController: BaseTableViewController {

    //属性
    private let ReuseIdentifier = "ReuseIdentifier"
    
    //微博数据
    var statuses: [Status]? {
        didSet{
            self.tableView.reloadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

       //登录以后才做后面的操作
        if !UserAccountViewModel.shareInstance.userLogin{
            return
        }
        
        setupTitleButton()
        setupNavigationBar()
        perpareTableView()
        
        StatusViewModel.shareInstance.loadStatus { (statuses, error) -> () in
            if error != nil{
                return
            }
            self.statuses = statuses
        }
    }
    
    //设置导航栏title
    private func setupTitleButton() {
        let button = HomeTitleButtonView(title: "sister", imageName: "navigationbar_arrow_down")
        button.addTarget(self, action: "didClickTitleButton:", forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = button
        
    }
    
    private func perpareTableView() {
        //注册cell
        tableView.registerClass(StatusCell.self, forCellReuseIdentifier: ReuseIdentifier)
        
        tableView.rowHeight = 80
    }
    
    //设置导航栏左右
    private func setupNavigationBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: "didClickLeftItem")
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: "didCilckRightItem")
    }

    //按钮点击事件
    @objc private func didClickTitleButton(button: HomeTitleButtonView) {
        button.selected = !button.selected
        
        UIView.animateWithDuration(DefaulDuration) { () -> Void in
            button.imageView?.transform = button.selected ?
            CGAffineTransformMakeRotation(CGFloat(M_PI - 0.00001)) :
            CGAffineTransformIdentity
        }
        if button.selected{
            //iOS8 以后popoverPresentationController
            let popoverVC = PopoViewController()
            
            //设置modal的显示样式
            popoverVC.modalPresentationStyle = .Popover
            
            //设置popoverPresentationController的代理。实现代理的方法，来让popover不要适配iphone
            popoverVC.popoverPresentationController?.delegate = self
            
            //popoVer的箭头指向titleView，默认指向左上角
            popoverVC.popoverPresentationController?.sourceView = navigationItem.titleView
            
            //设置surceView.bounds给sourceRect
            popoverVC.popoverPresentationController?.sourceRect = navigationItem.titleView!.bounds
            
            //设置popover显示的大小
            popoverVC.preferredContentSize = CGSize(width: 200, height: 44 * popoverVC.titles.count)
            
            //弹出
            presentViewController(popoverVC, animated: true, completion: nil)
        }
    }
    
    func didClickLeftItem() {
        
    }
    func didCilckRightItem() {
        
    }
}
//MARK: -- 扩展 HomeViewController 将tableView相关的方法都写在这个扩展里面
extension HomeViewController {
    //千万不要写到这个方法，返回多少组
    
    //返回多少个cellstatus
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses?.count ?? 0
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! StatusCell
        
        //设置cell的内容
        //text:String?
        cell.status = statuses?[indexPath.row]
        
        return cell
    }
    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return false
    }
}




//MARK: - 扩展 homeViewController 实现UIPopoverPresentationControllerDelegate协议
extension HomeViewController: UIPopoverPresentationControllerDelegate {
    //不让popover适配iPhone 默认适配 全屏的 不适配就是popover的效果
     func adaptivePresentationStyleForPresentationController(controller: UIPresentationController,traitCollection: UITraitCollection) ->UIModalPresentationStyle {
        return UIModalPresentationStyle.None
    }
    
    //系统会询问我们是否需要关闭popover
    @objc func popoverPresentationControllerShouldDismissPopover(popoverPresentationController:UIPopoverPresentationController) -> Bool {
        //让箭头转下来
        let button = navigationItem.titleView as! HomeTitleButtonView
        didClickTitleButton(button)
        return true
    }
    
}
